package com.sr.examination.user.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sr.examination.R;
import com.sr.examination.net.Net;
import com.tl.commonlibrary.network.ErrorResponse;
import com.tl.commonlibrary.network.RequestListener;
import com.tl.commonlibrary.network.bean.base.BaseBean;
import com.tl.commonlibrary.tool.CountDownTimer;
import com.tl.commonlibrary.tool.UIUtils;
import com.tl.commonlibrary.ui.BaseFragmentActivity;
import com.tl.commonlibrary.ui.beans.VerifyCodeBean;

import java.lang.reflect.Type;
import java.util.List;

import retrofit2.Call;

/**
 * Created by JongSung on 18/2/25.
 */

public class RegisterActivity extends BaseFragmentActivity {
    public static void start(Context context) {
        Intent starter = new Intent(context, RegisterActivity.class);
        context.startActivity(starter);
    }

    public static void startForPassword(Context context) {
        Intent starter = new Intent(context, RegisterActivity.class);
        starter.putExtra("resetPassword", true);
        context.startActivity(starter);
    }

    private CountDownTimer codeTimer;
    private EditText phoneEText, smsEText;
    private Button smsBtn;
    private ImageButton clearPhoneBtn;
    private boolean resetPassword = false;
    private String verifyCode = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_register);
        if (getIntent() != null) {
            resetPassword = getIntent().getBooleanExtra("resetPassword", false);
        }
        if (resetPassword) {
            setTitle(getString(R.string.reset_password));
        } else {
            setTitle(getString(R.string.register));
        }
        phoneEText = findViewById(R.id.phoneEText);
        smsEText = findViewById(R.id.smsEText);
        clearPhoneBtn = findViewById(R.id.clearPhoneBtn);
        smsBtn = findViewById(R.id.smsBtn);
        smsBtn.setOnClickListener(this);
        clearPhoneBtn.setOnClickListener(this);
        findViewById(R.id.nextBtn).setOnClickListener(this);
        phoneEText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s)) {
                    clearPhoneBtn.setVisibility(View.VISIBLE);
                } else {
                    clearPhoneBtn.setVisibility(View.GONE);
                }
            }
        });
        phoneEText.setText("");
    }

    private void getVerifyCode() {
        String phone = phoneEText.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            UIUtils.showToast(getString(R.string.hint_verify_code_null_phone));
            return;
        }
        if (phone.length() < 11) {
            UIUtils.showToast(getString(R.string.hint_input_phone_valid));
            return;
        }
        phoneEText.setEnabled(false);
        clearPhoneBtn.setVisibility(View.INVISIBLE);
        Net.getVerifyCode(phone, new RequestListener<BaseBean<VerifyCodeBean>>() {
            @Override
            public void onSuccess(Call<BaseBean<VerifyCodeBean>> call, BaseBean<VerifyCodeBean> response) {
                verifyCode = response.data.getVerifyCode();
                UIUtils.showToast(getString(R.string.get_verify_code_success));
                startVerifyCodeTimer();
            }

            @Override
            public void onFailed(Call<BaseBean<VerifyCodeBean>> call, ErrorResponse errorResponse) {
                phoneEText.setEnabled(true);
                clearPhoneBtn.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.clearPhoneBtn:
                phoneEText.setText("");
                break;
            case R.id.smsBtn:
                getVerifyCode();
                break;
            case R.id.nextBtn:
                next();
                break;
        }

    }

    private void next() {
        String phone = phoneEText.getText().toString().trim();
        String sms = smsEText.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            UIUtils.showToast(getString(R.string.hint_verify_code_null_phone));
            return;
        }
        if (TextUtils.isEmpty(sms)) {
            UIUtils.showToast(getString(R.string.hint_verify_code_null));
            return;
        }
        RegisterPasswordActivity.start(context, phone, sms, verifyCode, resetPassword);
    }

    private void startVerifyCodeTimer() {
        if (codeTimer == null) {
            codeTimer = new CountDownTimer(60 * 1000, 1000);
            codeTimer.setOnCountDownListener(new CountDownTimer.OnCountDownListener() {
                @Override
                public void onTick(long millisUntilFinished) {
                    smsBtn.setEnabled(false);
                    smsBtn.setText((millisUntilFinished / 1000) + "秒");
                }

                @Override
                public void onFinish() {
                    smsBtn.setEnabled(true);
                    smsBtn.setText(R.string.obtain_verify_code);
                }
            });
        }
        codeTimer.start();
    }

    @Override
    public void onFragmentTrigger(int tag, Bundle bundle) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (codeTimer != null) {
            codeTimer.cancel();
            codeTimer.setOnCountDownListener(null);
            codeTimer = null;
        }
    }
}
